package com.jamal2362.apktool.fragment.files;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.core.content.ContextCompat;
import java.io.File;
import com.jamal2362.apktool.R;
import com.jamal2362.apktool.fragment.FilesFragment;
import com.jamal2362.apktool.lexer.LexerUtil;
import com.jamal2362.apktool.task.FileSizeTask;
import com.jamal2362.apktool.task.IconTask;
import com.jamal2362.apktool.util.Colors;
import com.jamal2362.apktool.util.FileUtils;
import com.jamal2362.apktool.util.IconFactory;
import com.jamal2362.apktool.util.IconTypes;
import com.jamal2362.apktool.util.Settings;

public class FileItem implements Item {
	private final File file;
	private Context context;
	private IconFactory iconFactory;
	private boolean preventClick;

	public FileItem(File file, Context ctx) {
		this.file = file;
		this.context = ctx;
		this.preventClick = false;
		this.iconFactory = new IconFactory(ctx);
		this.iconFactory.round(Settings.roundIcons);
	}

	@Override
	public void setup(View root, ImageView icon, TextView name, TextView details, final Refreshable refresh) {
		if (refresh.isSelected(file.getAbsolutePath())) {
			int selectedColor = ContextCompat.getColor(context, (R.color.accent_material));
			root.setBackgroundColor(selectedColor);
		} else {
			root.setBackgroundColor(Color.TRANSPARENT);
		}
		boolean highLight = refresh.isHighlight(file.getAbsolutePath());
		icon.setTag(file.getAbsolutePath());
		String fileName = file.getName();
		if (highLight) {
			SpannableString spanFileName = new SpannableString(fileName);
			spanFileName.setSpan(new ForegroundColorSpan(Colors.DARK_GREEN), 0, fileName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			spanFileName.setSpan(new StyleSpan(Typeface.BOLD), 0, fileName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			name.setText(spanFileName);
		} else {
			name.setText(fileName);
		}
		FileSizeTask fileSize = new FileSizeTask(context, details, highLight);
		fileSize.execute(file);
		fileName = fileName.toLowerCase();
		if (file.isDirectory()) {
			int folderColor = (Settings.lightTheme ? IconTypes.colorFolderLight : IconTypes.colorFolderDark);
			icon.setImageBitmap(iconFactory.makeIcon(folderColor, R.drawable.ic_folder));
		} else {
			int[] types = IconTypes.getType(fileName);
			icon.setImageBitmap(iconFactory.makeIcon(types[1], types[0]));
			if (fileName.endsWith(".apk") || fileName.endsWith(".apks") || fileName.endsWith(".xapk") || types[0] == R.drawable.ic_image) {
				IconTask itask = new IconTask(context, icon);
				itask.execute(file);
			}
		}
		icon.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View p1) {
					click(p1, refresh);
				}
			});
		icon.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View p1) {
					refresh.setSelectionMode(true);
					refresh.setSelection(file.getAbsolutePath());
					preventClick = true;
					return true;
				}
			});
	}

	@Override
	public boolean edit(FilesFragment frag) {
		File file = this.file;
		if (file.isFile()) {
			String type = FileUtils.parseFileName(file.getName())[1];
			if (FileUtils.isText(file) || LexerUtil.isText(type)) {
				frag.edit(file);
				return true;
			}
		}
		return false;
	}

	public static boolean isJKS(String n) {
		return n.endsWith(".jks") || n.endsWith(".keystore");
	}

	public static boolean isKey(String n) {
		return n.toLowerCase().matches(".*\\.(jks|keystore|p12|pk8|x509|pem)");
	}

	@Override
	public boolean click(View view, Refreshable refresh) {
		refresh.setScrollPosition(file.getParent());
		if (preventClick) {
			preventClick = false;
			return true;
		}
		if (refresh.isSelectionMode()) {
			refresh.setSelection(file.getAbsolutePath());
			return true;
		}
		if (file.isDirectory()) {
			return false;
		}
		FileUtils.open(file, view, refresh);
		return true;
	}

	@Override
	public void process(FilesAdapter adapter) {
		adapter.refresh(file);
	}

	@Override
	public boolean longClick(View view, Refreshable refresh) {
		refresh.setScrollPosition(file.getParent());
		FileUtils.file(file, view, refresh);
		return true;
	}

	@Override
	public int compareTo(Item p1) {
		if (!(p1 instanceof FileItem))
			return 1;
		File f1 = file;
		File f2 = ((FileItem)p1).file;
		if (f1.isDirectory() && f2.isFile())
			return -1;
		if (f2.isDirectory() && f1.isFile())
			return 1;
		return FileComparator.getDefaultAdapter().compare(f1, f2);
	}

	public int getProperty() {
		return PROPERTY_FILE;
	}
}
